package com.zhufeng.web.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.zhufeng.base.api.ShowUserApi;
import com.zhufeng.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: MsgController
 * @Description 消息测试
 * @author 月夜烛峰
 * @date 2022/7/22 10:33
 */
@RequestMapping("msg")
@RestController
public class MsgController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    ShowUserApi showUserApi;

    @RequestMapping("info")
    public UserInfo showInfo(@RequestBody UserInfo user) {
        System.out.println(JSONObject.toJSONString(user));
        user.setUserId("zf1002");
        return user;
    }

    @RequestMapping(value = "json", method = RequestMethod.POST)
    public String showJsonMsg( HttpServletRequest request) {
        JSONObject json = new JSONObject();
        try {
            String responseString = readInputStream(request.getInputStream());
            System.out.println(responseString);
            json = JSONObject.parseObject(responseString);
            json.put("good","luck~");
            System.out.println("show json");
        }catch (Exception e){
            e.printStackTrace();
        }

        return json.toJSONString();
    }

    private String readInputStream(InputStream inputStream) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String tmp;
        StringBuilder sb = new StringBuilder();
        while ((tmp = reader.readLine()) != null) {
            sb.append(tmp).append("\n");
        }
        if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
            sb.setLength(sb.length() - 1);
        }
        reader.close();
        return sb.toString();
    }

    @RequestMapping("showUser")
    public String showUser() {
        //请求参数，仅作为演示参数传递
        Map<String,String> reqParam = new HashMap<String,String>();
        reqParam.put("userId", "zf001");
        //jcold-route-user 为访问的微服务名称
        String pref = "http://zhufeng-web-user";
        //uri为微服务中访问的地址
        String uri = "/rest/user";
        ResponseEntity<String> res = restTemplate.postForEntity(pref+uri, reqParam, String.class);
        return res.getBody();
    }

    @RequestMapping("postUser")
    public String postUser() {
        //请求参数，仅作为演示参数传递
        UserInfo user = new UserInfo();
        user.setUserId("zf1001");
        user.setUserName("月夜烛峰");
        //zhufeng-gateway-db 为访问的微服务名称
        String pref = "http://zhufeng-gateway-db";
        //uri为微服务中访问的地址
        String uri = "/msg/user";
        ResponseEntity<UserInfo> res = restTemplate.postForEntity(pref+uri, user, UserInfo.class);

        return JSONObject.toJSONString(res.getBody());
    }



    @GetMapping("/fegin/{id}")
    public JSONObject showUserById(@PathVariable("id") int id) {

        JSONObject userJson = new JSONObject();
        userJson.put("status", "success");
        userJson.put("name","zhufeng");
        return userJson;
    }
}
